﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementDetailList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
               // UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
                //设置经办人
               Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
             //   this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                this.BindAccountID();
                this.BindTypeID();
                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
                this.txtDateSearchStart.Attributes["readonly"] = "true";
                this.txtDateSearchStart.Date = System.DateTime.Now;
                this.txtDateSearchEnd.Attributes["readonly"] = "true";
                this.txtDateSearchEnd.Date = System.DateTime.Now;
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");

            }
        }
        private void BindAccountID()
        {
           var bll = mk.l.Sales_Account.ToList();
            var ds = bll.Where(y=>y.RootDepartmentID.ToString()== this.lblRootDepartmentID.Text).ToList();
            this.ddlAccountID.DataSource = ds;
            this.ddlAccountID.DataTextField = "Name";
            this.ddlAccountID.DataValueField = "AccountID";
            this.ddlAccountID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlAccountID.Items.Add(li);
            this.ddlAccountID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// Binds the type ID.
        /// </summary>
        private void BindTypeID()
        {
           var bll = mk.l.Sales_SettlementType.ToList();
           // DataSet ds = new DataSet();
           IEnumerable<Sales_SettlementType> ds = null;
            if (this.ddlFlag.SelectedValue != "0")
            {
               ds = bll.Where(y=>y.Flag==this.ddlFlag.SelectedValue);
            }
            else
            {
               ds = bll.ToList();
            }
            this.ddlTypeID.DataSource = ds;
            this.ddlTypeID.DataTextField = "Name";
            this.ddlTypeID.DataValueField = "TypeID";
            this.ddlTypeID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlTypeID.Items.Add(li);
            this.ddlTypeID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// 绑定明细列表
        /// </summary>
        private void BindData()
        {
         //   UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
            var ds = mk.l.Sales_SettlementDetail.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        /// <summary>
        /// 绑定数据时显示状态信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //显示账户名称
                Label lblAccountName = (Label)(e.Row.FindControl("lblAccountName"));
              var bllAccount = mk.l.Sales_Account.ToList();
            Sales_Account mAccount = bllAccount.Where(h=>h.AccountID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["AccountID"].ToString())).FirstOrDefault();
                if (mAccount != null)
                {
                    lblAccountName.Text = mAccount.Name;
                }

                //显示收支类别
                Label lblTypeName = (Label)(e.Row.FindControl("lblTypeName"));
                var bllSettlementType =  mk.l.Sales_SettlementType.ToList();
              Sales_SettlementType mSettlementType = bllSettlementType.Where(h=>h.TypeID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["TypeID"].ToString())).FirstOrDefault();
                if (mSettlementType != null)
                {
                    lblTypeName.Text = mSettlementType.Name;
                }

                //显示经办人姓名
                Label lblManager = (Label)(e.Row.FindControl("lblManager"));
              var  bllUser = mk.l.Accounts_Users.ToList();
           Accounts_Users mUser = bllUser.Where(h=>h.UserName==this.myGridView.DataKeys[e.Row.RowIndex].Values["Manager"].ToString()).FirstOrDefault();
                lblManager.Text = mUser.TrueName;


                //显示状态
                Label lblFlag = (Label)(e.Row.FindControl("lblFlag"));
                string strFlag = this.myGridView.DataKeys[e.Row.RowIndex].Values["Flag"].ToString();
                switch (strFlag)
                {
                    case "1":
                        lblFlag.Text = "支出";
                        break;
                    case "2":
                        lblFlag.Text = "收入";
                        break;
                    default:
                        lblFlag.Text = "未知";
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }


        /// <summary>
        /// Handles the Click event of the btnSetPageSize control.设置分页页数
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlFlag control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlFlag_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindTypeID();
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.查询相关订单信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
           
            this.BindData();
        }

        /// <summary>
        /// Handles the DataBound event of the myGridView control.计算汇总数据
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void myGridView_DataBound(object sender, EventArgs e)
        {
            //GridViewRow footer = this.myGridView.FooterRow;
            //if (footer != null)
            //{
            //    Label lblAmountInput = footer.FindControl("lblAmountInput") as Label;
            //    Label lblAmountOutput = footer.FindControl("lblAmountOutput") as Label;
            //    Label lblAmount = footer.FindControl("lblAmount") as Label;
            //  var bll = mk.l.Sales_SettlementDetail.ToList();
            //    lblAmountInput.Text = bll.Where(y=>y.this.lblStrWhere.Text).ToString("c");
            //    lblAmountOutput.Text = bll.GetSettlementAmountOutput(this.lblStrWhere.Text).ToString("c");
            //    lblAmount.Text = bll.GetSettlementAmount(this.lblStrWhere.Text).ToString("c");
            //}
        }


      
    }
}
